<!--
 * @Author: YMJ
 * @Date: 2020-10-30 10:48:20
-->
<template>
  <div>
    <tab-list
      ref="list"
      :searchs="searchs"
      :list-hedar="listHedar"
      :params="params"
      :api-function="apiFunction"
      other-show
      @init-params="initParams"
      @add-list="openEditBox()"
    >
      <template slot="operate" slot-scope="scope">
        <el-button type="text" size="small" @click="openEditBox(scope.value.id)">编辑</el-button>
        <el-button type="text" size="small" @click="deleteItem(scope.value.id)">删除</el-button>
      </template>
    </tab-list>
    <maeesge-box ref="box" @submit="submit">
      <editFromList ref="edit" :editList="editList" :params="editParams"/>
    </maeesge-box>
  </div>
</template>
<script>
// 组件
import tabList from '@/components/admin/tab-list.vue';
import editFromList from '@/components/admin/edit-from-list.vue';
import maeesgeBox  from '@/components/admin/maeesge-box';
// 公共方法
import { asyncApiFun, openMessageBox } from '@/utils/utils'
// 接口
import { versionAll, versioEdit, versionDetail, versioDel } from '@/api/basics.js'
// 页面配置
import { listHedar, searchs, createEdit } from './config/setMeal.config.js'
export default {
  components: {
    tabList,
    editFromList,
    maeesgeBox
  },
  data() {
    return {
      toastShow: false,
      listHedar,
      searchs,
      params: {},
      editParams: {},
      editList: createEdit(),
      apiFunction: versionAll,
      detail: null
    }
  },
  methods: {
    async submit() {
      const [res] = await asyncApiFun(versioEdit, this.$refs.edit.ruleFrom());
      if (res) {
        this.$message.success('操作成功')
        this.$refs.box.close();
        this.$refs.list.getList();
      }
    },
    async openEditBox(id) {
      let _detail = null;
      let _params = {};
      if (id) {
        const [res] = await asyncApiFun(versionDetail, id);
        if (res) {
          _params = { id },
          _detail = res.data;
        }
      }
      this.editParams = _params;
      this.editList = createEdit(_detail);
      this.$refs.box.open();
    },
    // 删除
    deleteItem(id) {
      openMessageBox({
        content: '删除数据后，将不可恢复，是否确认？',
        title: '删除数据'
      }, async boole => {
        if (boole) {
          const [res] = await asyncApiFun(versioDel, id)
          if (res) this.$refs.list.getList()
        }
      })
    },
    initParams(param) {
      this.params = param
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
